package lolic;

import java.io.File;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 * This class holds the information for each item in the game.
 * @author Dave DePiano
 *
 */
public class Item 
{
	int intCost;
	ImageIcon imgPortrait;
	String strName;
	int intID;
	
	public ImageIcon getImgPortrait() {
		return imgPortrait;
	}


	public void setImgPortrait(ImageIcon imgPortrait) {
		this.imgPortrait = imgPortrait;
	}
	
	public String getStrName() {
		return strName;
	}


	public void setStrName(String strName) {
		this.strName = strName;
	}


	public int getIntID() {
		return intID;
	}


	public void setIntID(int intID) {
		this.intID = intID;
	}
	
	public String toString()
	{
		return getStrName();
	}


	public Item(String strName, int intID)
	{
		setIntID(intID);
		setStrName(strName);
	}
	
	public Item(String strName, int intID, ImageIcon imgIcon)
	{
		setIntID(intID);
		setStrName(strName);
		setImgPortrait(imgIcon);
	}
	
	/**
	 * Need to use this function since getSelectedIndex() doesn't work...
	 * @param strItem
	 * @param list
	 * @return
	 */
	
	public static Item getMatchingItem(String strItem, List list)
	{
		boolean found = false;
		int i = 0;
		while(found == false && i < list.size())
		{
			Item item = (Item)list.get(i);
			Integer integer = new Integer(item.getIntID());
			if( (item.getStrName().equalsIgnoreCase(strItem))  || (integer.toString().equalsIgnoreCase(strItem)) )
			{
				found = true;
				return item;
			}
			i++;
		}
		return new Item("",0, new ImageIcon("blank.gif")); //didn't find an item...return a blank one instead of null
	}
	
	public static File getIniFile(Champ champ, File fBasePath)
	{
		File fIniFile = new File(".");
		//special case for non US/EU clients
		if(fBasePath.getAbsolutePath().endsWith("Characters"))
		{
			File fCompletePath = new File(fBasePath.getAbsoluteFile().getAbsolutePath() + champ.getStrName());
			fIniFile = getIniFile(fCompletePath);
		}
		else
		{
		String strAlmostCompletePath = fBasePath + "\\League of Legends\\RADS\\solutions\\lol_game_client_sln\\releases\\";

		File fAlmostCompletePath = new File(strAlmostCompletePath);
		File aryFiles[] = fAlmostCompletePath.listFiles();
		if(aryFiles == null)
		{
			JOptionPane.showMessageDialog(GUI.frame, "There's something wrong with your browse path, make sure you are pointing to the right place",
					"Error saving file", JOptionPane.ERROR_MESSAGE);
			return new File(".");
		}
		
		//get the folder which was modified last, this is the latest version
		File fLastModified = aryFiles[0];
		long lngModTime = 0;
		for (int i=0;i < aryFiles.length; i++)
		{
			lngModTime = aryFiles[i].lastModified();
			if(lngModTime > fLastModified.lastModified())
			{
				fLastModified = aryFiles[i];
			}
		}
		System.out.println(fLastModified.getAbsolutePath());

		File fCompletePath = new File(fLastModified.getAbsolutePath() + "\\deploy\\Data\\Characters\\" + champ.getStrName());
		fIniFile = getIniFile(fCompletePath);
		}
		return fIniFile;
	}


	private static File getIniFile(File fCompletePath) {
		File fIniFile;
		fCompletePath.mkdirs(); //create the directories if they don't exist

		String strFileAppendix;
		if(GUI.cmbGameType.getSelectedIndex() == 0)
		{
			strFileAppendix = "CLASSIC";
		}
		else
		{
			strFileAppendix = "ODIN";
		}

		fIniFile = new File(fCompletePath.getAbsolutePath() + "\\RecItems" + strFileAppendix + ".ini");
		return fIniFile;
	}
}

